package it.com.Test.a1_async;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class TestOfWhenComplete {

    static ExecutorService executor = Executors.newFixedThreadPool(3);

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        // 有返回值
        System.out.println("main....start....");
        CompletableFuture<Integer> completableFuture = CompletableFuture.supplyAsync(() -> {
            System.out.println("当前线程：" + Thread.currentThread().getId());
//            int i = 10 / 2;
             int i = 10 / 0;
            System.out.println("运行结果：" + i);
            return i;
        }, executor).whenComplete((r, e) -> {
            System.out.println("whenComplete-result = " + r + " ，感知到的异常是： " + e);
        }).exceptionally(t -> {
            // 感知到异常，同时返回默认值
            return 10;
        });
        System.out.println("main....end...." + completableFuture.get());
/**
 main....start....
 当前线程：12
 运行结果：5
 whenComplete-result = 5 ，感知到的异常是： null
 main....end....5
 ----------------------------------------------------
 main....start....
 当前线程：12
 whenComplete-result = null ，感知到的异常是： java.util.concurrent.CompletionException: java.lang.ArithmeticException: / by zero
 main....end....10
  */

    }
}
